﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Xarmarin.GoldFinger.Models;

namespace Xarmarin.GoldFinger.Engine.Rand
{
    internal class RandCore
    {
        private static Random random = new Random();
        public static long ManaRandValue => random.Next(10, 200);
        /// <summary>
        /// 带权重随机获取随机列表
        /// </summary>
        /// <param name="list">原始列表</param>
        /// <param name="count">随机抽取条数</param>
        /// <returns></returns>
        public static List<T> GetRandomList<T>(List<T> list, int count) where T : RandObject
        {
            if (list == null || list.Count <= count || count <= 0)
            {
                return list;
            }
            //计算权重总和
            int totalWeights = 0;
            for (int i = 0; i < list.Count; i++)
            {
                totalWeights += list[i].Weight + 1;
            }
            //随机赋值权重 
            //GetRandomSeed()随机种子，防止快速频繁调用导致随机一样的问题
            Random ran = new Random(GetRandomSeed());  
            //Random ran = new Random(DateTime.Now.Millisecond);
            List<KeyValuePair<int, int>> wlist = new List<KeyValuePair<int, int>>();    //第一个int为list下标索引、第一个int为权重排序值
            for (int i = 0; i < list.Count; i++)
            {
                int w = (list[i].Weight + 1) + ran.Next(0, totalWeights);
                wlist.Add(new KeyValuePair<int, int>(i, w));
            }

            KeyValuePair<int, int> temp;
            bool flag = false;
            //冒泡优化
            for (int i = 0; i < wlist.Count; i++)
            {
                flag = false;
                for (int j = 0; j < wlist.Count - i - 1; j++)
                {
                    if(wlist[j].Value > wlist[j + 1].Value) {

                        temp = wlist[j];
                        wlist[j] = wlist[j+1];
                        wlist[j+1] = temp;
                        flag = true;
                    }
                }
                if (flag == false)//上一个循环比较结束而没有发生交换，但是每两个相邻元素都比较过了，说明已经有序
                {
                    break;//已经有序跳出循环
                }
            }
            //冒泡排序
            //for (int i = 0; i < wlist.Count; i++)
            //{
            //    for (int j = 0; j < i; j++)
            //    {
            //        if (wlist[i].Value > wlist[j].Value)
            //        {
            //            temp = wlist[i];
            //            wlist[i] = wlist[j];
            //            wlist[j] = temp;
            //        }
            //    }
            //}

            //根据实际情况取排在最前面的几个
            List<T> newList = new List<T>();
            for (int i = 0; i < count; i++)
            {
                T entiy = list[wlist[i].Key];
                newList.Add(entiy);
            }
            return newList;
        }
        /// <summary>
        /// 不带权重随机
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<T> GetRandomListWithOutWeight<T>(List<T> list,int count)
        {
            if (list == null || list.Count <= count || count <= 0)
            {
                return list;
            }
            //计算权重总和
            int totalWeights = 0;
            for (int i = 0; i < list.Count; i++)
            {
                totalWeights += 1;
            }
            //随机赋值权重 
            //GetRandomSeed()随机种子，防止快速频繁调用导致随机一样的问题
            Random ran = new Random(GetRandomSeed());
            //Random ran = new Random(DateTime.Now.Millisecond);
            List<KeyValuePair<int, int>> wlist = new List<KeyValuePair<int, int>>();    //第一个int为list下标索引、第一个int为权重排序值
            for (int i = 0; i < list.Count; i++)
            {
                int w = 1 + ran.Next(0, totalWeights);
                wlist.Add(new KeyValuePair<int, int>(i, w));
            }

            KeyValuePair<int, int> temp;
            bool flag = false;
            //冒泡优化
            for (int i = 0; i < wlist.Count; i++)
            {
                flag = false;
                for (int j = 0; j < wlist.Count - i - 1; j++)
                {
                    if (wlist[j].Value > wlist[j + 1].Value)
                    {

                        temp = wlist[j];
                        wlist[j] = wlist[j + 1];
                        wlist[j + 1] = temp;
                        flag = true;
                    }
                }
                if (flag == false)//上一个循环比较结束而没有发生交换，但是每两个相邻元素都比较过了，说明已经有序
                {
                    break;//已经有序跳出循环
                }
            }
            //根据实际情况取排在最前面的几个
            List<T> newList = new List<T>();
            for (int i = 0; i < count; i++)
            {
                T entiy = list[wlist[i].Key];
                newList.Add(entiy);
            }
            return newList;
        }
        /// <summary>
        /// 普通随机
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetRandString(List<string> list)
        {
            Random ran = new Random(GetRandomSeed());
            return list[ran.Next(0, list.Count)];
        }
        /// <summary>
        /// 普通随机值
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static long GetRandValue(int start=1,int end=10)
        {
            return random.Next(start, end);
        }

        /// <summary>
        /// 随机种子值
        /// </summary>
        /// <returns></returns>
        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
    }
}
